//
//  LYCompanyCell.m
//  SelectCity
//
//  Created by liuyan on 2019/3/14.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYCompanyCell.h"
#import "LYCompanyItemCell.h"

@interface LYCompanyCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

/**
 每个大分类的collectview
 */
@property (nonatomic, strong) UICollectionView *collectview;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *itemDataArray;
@property (nonatomic, assign) NSInteger index;

@end

@implementation LYCompanyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubViews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

#pragma mark - InitSubViews
- (void)initSubViews {
    self.backgroundColor = COLOR_OF_CLEAR;
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, WIDTH - 80, 50.0)];
    _titleLabel.font = FontBold(Font_15);
    _titleLabel.textColor = THEMECOLOR_OF_TEXT_333333;
    [self addSubview:_titleLabel];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat width = (WIDTH - 60)/3;
    layout.itemSize = CGSizeMake(width, 35);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 15;
    _collectview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, EndY(_titleLabel), WIDTH, 45.0) collectionViewLayout:layout];
    _collectview.backgroundColor = COLOR_OF_CLEAR;
    _collectview.delegate=self;
    _collectview.dataSource=self;
//        //注册item类型 这里使用系统的类型
    [_collectview registerClass:[LYCompanyItemCell class] forCellWithReuseIdentifier:@"LYCompanyItemCell"];
    [self addSubview:_collectview];
}

#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.dataArray && self.dataArray.count) {
        return self.dataArray.count;
    }
    return self.itemDataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LYCompanyItemCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"LYCompanyItemCell" forIndexPath:indexPath];
    cell.backgroundColor = COLOR_OF_CLEAR;
    if (self.dataArray && self.dataArray.count) {
        NSDictionary *dic = self.dataArray[indexPath.row];
        [cell setTitle:dic[@"name"] isSelect:(self.index == indexPath.row)?YES:NO];
    } else {
        PublicCategoryElementModel *model = self.itemDataArray[indexPath.row];
        [cell setTitle:model.name isSelect:((self.index == indexPath.row)?YES:NO)];
    }
    return cell;
}

#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (self.dataArray && self.dataArray.count) {
        _block(indexPath.row);
    } else {
        self.backlock(indexPath.row);
    }
}
#pragma mark -- UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15, 0,15);
}
- (void)setData:(NSDictionary *)dataDic selectIndex:(NSInteger)index{
    self.index = index;
    _titleLabel.text = dataDic[@"name"];
    self.dataArray = dataDic[@"child"];
    CGFloat height = 0;
    if (self.dataArray.count % 3 == 0) {
        height = height + self.dataArray.count/3 * 55;
    } else {
        height = height + (self.dataArray.count/3 + 1) * 55;
    }
    _collectview.frame = CGRectMake(0, EndY(_titleLabel), WIDTH, height);
    [_collectview reloadData];
}
- (void)reloadData:(PublicCategoryElementModel *)model selectIndex:(NSInteger)index {
    self.index = index;
    _titleLabel.text = model.name;
    self.itemDataArray = model.childArray;
    CGFloat height = 0;
    if (self.itemDataArray.count % 3 == 0) {
        height = height + self.itemDataArray.count/3 * 55;
    } else {
        height = height + (self.itemDataArray.count/3 + 1) * 55;
    }
    _collectview.frame = CGRectMake(0, EndY(_titleLabel), WIDTH, height);
    [_collectview reloadData];
}

@end
